Skip to content

cmake: optional wrapper units built from function-visibility.txt - #399

Merged
TheRedDaemon merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/function-visibility-wrappers
Nov 21, 2026
Merged

TheRedDaemon merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/function-visibility-wrappers

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Adds an optional way to build the units that cmake/function-visibility.txt describes, instead of relying on /GL. It builds on #398, the manifest, so its diff holds #398's commits too: please review #398 first. Off by default: without the option, nothing changes.

cmakew --preset RelWithDebInfo -DOPENSHC_FUNCTION_VISIBILITY_UNITS=ON

What it does

At configure time, cmake/function-visibility-units.py reads the manifest and groups every caller with the callees whose bodies it must see. A function can be defined only once, so everything joined by entries ends up in one group. For each group it writes one wrapper .cpp into the build folder:

// Generated from cmake/function-visibility.txt by cmake/function-visibility-units.py. Do not edit.
#include "C:/.../src/OpenSHC/Map/Units/UnitsState/checkIfCitizenUnitIsAliveBasedOnState.cpp"
#include "C:/.../src/OpenSHC/Map/Units/UpdateCamel.cpp"
...

CMakeLists.txt then builds the wrappers in place of their members. The members come off the /GL list, and the wrappers themselves are built without /GL. The source tree is not touched.

  • No .cpp is included twice: groups are formed over files, not functions, so a file that defines several listed functions lands in exactly one wrapper.
  • Name clashes: two members defining the same internal-linkage name (a file-scope static, or anything in an anonymous namespace) would not compile in one unit. Such a group is not wrapped: its files build as before, and the clash is printed at configure time. The check reads the source line by line rather than parsing it, so a brace inside a string or a comment could hide a static from it. A clash it misses fails loudly (C2084). Today src/OpenSHC has one file-scope static and no anonymous namespaces.
  • Missing names: manifest names that no .cpp in the build defines are skipped, and configure prints how many and which. On main today that is the 22 names cmake: function-visibility.txt, which callers must see which callees' bodies #398 lists.
  • Python: the option needs Python at configure time (find_package(Python3)). Under a preset that replaces PATH, CMake may not find it; pass -DPython3_EXECUTABLE=<path to python.exe> then.

Does it reproduce /GL?

Measured on our Map::Units branch (reimpl/units-B-development, which contains main 677469cd2, so main's whole /GL list is in it). One full build used /GL as today, one used this option with the manifest from #398. Both had every resolver switched on, and all files compiled. reccmp:

functions identical better worse 100% with /GL still 100%
in a wrapper 128 123 5 0 108 108
everything else 2,126 2,124 2 0 1,999 1,999
all 2,254 2,247 7 0 2,107 2,107

The seven that got better:

function /GL wrappers
Map::Units::UpdateDairyFarmer 93.9 100.0
Map::Units::UpdateSlave 81.8 88.8
Map::Units::UpdateMerchantHorse 51.5 72.5
Map::Units::UpdateMerchant 70.1 76.7
Map::Units::UpdateAppleFarmer 85.1 87.7
UI::Menu::Constructor_Menu 98.4 100.0
UI::Menu::handleMenuItems 97.9 100.0

UpdateDairyFarmer is the case /GL can't express: it must not see getResourceCountThatCanBeDeposited, but addResourceToGranary must, and under /GL every listed file sees every other. Every function that is 100% under /GL stays 100%, including UpdateCamel, UpdateLady, UpdateFireEater, UpdateInnkeeper and all the main /GL callers. UpdateHorseArcher_DoShooting and UpdateSiegeTent need no unit-mates, so they are not in a wrapper, and they stay 100% as well.

On main 3458fea34 it was built and measured too (update of 2026-11-21 below): 24 wrappers holding 92 files, and none of the 150 functions that can differ reads a different raw number than under /GL.

Things worth a look

  • One big wrapper. With every manifest name present, one wrapper holds 40 files: 37 from Map::Units, 2 from Map::Buildings and ViewportRenderState::xyAreValid. Almost every Map::Units Update function needs checkIfCitizenUnitIsAliveBasedOnState or hasUnitReachedDestination, which chains them all together. The two far edges still listed below pull in the rest. On main alone the largest wrapper is 18 files (the building-menu render functions).
  • Far edges. Three measured edges spanned most of the image. By the unit boundaries in tools/compiler-internals/gl-conundrum.md (section 4), each of these pairs has a boundary between them, so they can't share a file.
    • UpdateInnkeeper (0x540EC0) → BuildingsState::getBuildingResourceAmountByUid (0x409330): kept. Without it, Innkeeper drops from 100 to 97.3. It may be a body in a header (see "A third kind of visibility").
    • UpdateMerchant (0x5416B0) → GameStateStructures::activateTraderState (0x4586D0): removed. It was measured on a draft, and the 100% version on main must not see that body (update of 2026-11-21).
    • UpdatePikeman and UpdateMaceman (0x55F110, 0x560700) → ViewportRenderState::xyAreValid (0x401000, the image's first function): still listed, and skipped by the generator until the two callers land. To be measured again then.
  • /GL files outside every group. With the option on, 45 files on the /GL list are in no group and keep /GL, while the files they were in one /GL module with leave it. Four of the 45 don't exist on main (stale lines in compiler-flags-gl.txt, listed in cmake: function-visibility.txt, which callers must see which callees' bodies #398). The rest had no measured edge: nothing they call changed their score. None of them got worse in the build above. The rule: once a callee is in a wrapper, every caller that must see it needs an entry. On main with this manifest no such caller is left out: nothing outside the wrappers reads lower (update of 2026-11-21). It did happen once, with a hand-made manifest on our branch (update of 2026-11-20).
  • Generated paths are absolute. The wrappers live in the build folder and #include the members by absolute path, so moving the source tree needs a reconfigure, which happens anyway.

Update (2026-11-20): measured again, with #378's functions and a fuller manifest

A second measurement, on a different tree and with more files in the wrappers. With the option on, 327 of the 363 functions in the build read 100%, against 323 under /GL, and no function that reads 100% under /GL reads lower. Three unit updates reach 100% that /GL can only get there by breaking six functions on main.

What was measured

Result

12 of the 363 functions read differently between the two builds:

Function /GL Wrappers
BuildingsState::canUnitReachBuildingPerimeter 60.1 100.0
Units::UpdateBaker 93.2 100.0
Units::UpdateHunter 96.3 100.0
Units::UpdateDairyFarmer 93.9 100.0
Units::UpdateWoodcutter 95.5 99.2
Units::UpdateBlacksmith 92.6 98.9
Units::UpdateBrewer 89.8 95.2
Units::UpdateSlave 81.8 88.8
Units::UpdateAppleFarmer 85.1 87.7
WindowAndDirectDraw::takeScreenshot (unfinished) 44.5 44.8
Units::UpdateCatapult 86.8 86.5
UI::Rendering::InitBlendFilterArraysUnk 97.9 97.9 (normalised 97.5 → 95.0)
  • 327 functions read 100.0 with the wrappers, 323 under /GL.
  • No function that reads 100.0 under /GL reads lower with the wrappers.
  • UpdateCatapult is the only function that reads lower, by 0.3. It is not a missing entry: all 14 /GL files in its call closure are in its wrapper, and a /GL build of only Catapult and those 14 files reads the same 86.5. The 86.8 shows up only when the whole list is one /GL module, so it comes from something outside its own calls.
  • InitBlendFilterArraysUnk keeps its raw number; only the normalised one differs. It is a callee in two entries the manifest already had.

The manifest that was used

It was made by rule, not from measured edges: every Map::Units source on the /GL list gets an entry with each /GL callee it calls, except callees under Map/Buildings. The manifest of this PR is kept as it is. That gives 127 entries and 516 lines, where this PR has 209 lines. It is an upper bound: measured edges would be fewer.

The generator needed no change. It wrote 28 wrappers holding 186 of the sources. The largest has 94 files: 35 unit updates, 43 UnitsState functions, 2 files from Map::Buildings and 9 others. It compiled and linked first try.

These lines are not added to this PR. They name functions that only #378 has, and unit updates that aren't on main yet. They can follow once those are in.

A first, hand-measured manifest was tried before the rule: 26 lines, with entries for UpdateBaker and UpdateHunter and the callees they must see. It already put the three units at 100.0, but three other functions fell from 100.0: UnitsState::deleteUnit (98.7), UpdateOx (86.8) and UpdateGhost (49.3). Each calls a function that had moved into a wrapper while the caller stayed outside. So the caveat under "/GL files outside every group" is real: once a callee is wrapped, every caller that must see it needs an entry. With entries for them, all three read 100.0 again.

What it means

  • Per-caller visibility does something /GL can't. The unit updates must not see four BuildingsState bodies that other callers need: updateBuildingSignpostCounter, getResourceCountThatCanBeDeposited, getStorageBuildingForResourceTypeAndAmount and addResourceToStockpile. Under /GL every listed file sees every other, so UpdateBaker, UpdateHunter and UpdateDairyFarmer read 93.2, 96.3 and 93.9. Taking the four off the /GL list puts those three at 100.0, but six functions on main fall with it: addResourceToGranary 100 → 76.3, getStorageBuildingForResourceTypeAndAmount 100 → 58.8, getResourceSpace 100 → 88.9, addResourceToStockpile 100 → 97.9, UpdateBadBuildingDungeon 100 → 96.4, UpdateBadBuildingGallows 100 → 94.1. With the wrappers the three units and the six main functions all read 100.0: the four bodies sit in three small wrappers with the callers that need them, and the units' wrapper doesn't hold them.
  • The same in the other direction. canUnitReachBuildingPerimeter (on main) reads 60.1 in the /GL build of this merge, because Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 puts UnitsState/selectionContainsOnlyArabAssassins.cpp on the /GL list and the caller then sees a body the original didn't see. A second /GL build without that one line reads 100.0, and it is the only one of the 363 functions that differs between those two builds. In its own wrapper the function doesn't see that body either, and reads 100.0.
  • The build was faster. The two wrapper builds took 39 and 16 minutes, the /GL builds of the same sources between 43 and 86. Other builds ran on the same PC at times, so take it as an indication, not a benchmark.

Still open

  • A third kind of visibility. UpdateWoodcutter and UpdateBlacksmith stop one instruction short, at 99.2 and 98.9, under /GL and with the wrappers alike. Behind BuildingsState::getBuildingResourceAmountByUid the original loads ecx again but doesn't read the unit slot again: it knows what the callee writes, but has no register record for it. Neither /GL nor a wrapper can state that. In a local test with that one definition marked inline and included from its class header, both units read 100.0, and main's two callers (UpdateIronMine, UpdateInnkeeper) stay at 100.0. That touches a generated header, so it is a separate question; the write-up is ready.
  • Catapult's 0.3. What the whole-list /GL module gives UpdateCatapult beyond its own callees is not known. The function is at 86%, so nothing at 100% depends on it.
  • This run is not the whole tree. It built 350 sources. The whole-tree comparison is the one above, under "Does it reproduce /GL?".

Update (2026-11-21): built and measured on main

Builds of main, alone and with #378, with the option off and on. The build list is the /GL list plus every file the manifest names: every other file has the same compile command in both modes. reccmp, raw.

tree functions at 100.0 with /GL at 100.0 with the wrappers (manifest with #398's fix)
main 3458fea34 + this PR 150 146 146
main + #378 e81059741 + this PR 298 289 292

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 12, 2026 19:37
… bodies

One entry per caller, with the callees whose bodies it must see when it is
compiled, fully qualified as the definitions are written:

    OpenSHC::funcA
        OpenSHC::funcB

87 entries, 127 edges, sorted by the caller's address. Every edge is a
tu_merge.py measurement: pasting the callee into the caller's unit raises the
caller's quick_diff ratio, and for callers with several callees a leave-one-out
over the full set decided which are needed together. In all 127 the callee is
at a lower address than its caller.

Twelve measured gains are left out because they cannot be same-file edges:
in seven the callee is at a higher address than its caller, and the other
five would put a callee into a unit with callers that measure worse when they
see it.

23 names are not on main yet. 22 are Map::Units Update callers: they were
measured on our local branches reimpl/units-B-development and
reimpl/units-A-development (UpdateCamel is also on the local
reimpl/units-B-round2-100), and gynt's reimpl/map-units-updatefunctions has
drafts of all 22. The 23rd is UnitsState::changeDestinationByAmount from sourcehold#378.
A generator skips names it cannot find. The measurements are in the PR
description.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -DOPENSHC_FUNCTION_VISIBILITY_UNITS=ON, configure runs
cmake/function-visibility-units.py. It groups every caller of
cmake/function-visibility.txt with the callees whose bodies it must see (one
group per connected set, since a function is defined once) and writes one
wrapper .cpp per group that #includes the members. The wrappers replace their
members in the build, and the members leave the /GL list; the wrappers are
built without /GL. Off by default; nothing changes without the option.

Groups are formed over files, so a .cpp that defines several listed functions
is included once. A group whose members define the same internal-linkage name
(a file-scope static or anything in an anonymous namespace) is not wrapped,
and its files build as before. Manifest names with no .cpp in the build are
skipped and listed at configure time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kotijekriv and others added 3 commits November 14, 2026 00:14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tCounter's body

sourcehold#290 rewrote UpdateBadBuildingGibbet and UpdateBadBuildingStretchingRack so that they reach 100% without /GL. On
main (ea63a49), tu_merge.py gives both 1.0000 in all three layouts: the caller alone, the callee's body pasted
before it, and pasted after it (45 of 45 and 41 of 41 instructions). Seeing updateBuildingSignpostCounter no longer
helps either of them, so their two entries go. The callee keeps its other two callers, UpdateBadBuildingGallows and
UpdateBadBuildingDungeon.

The manifest now has 85 entries and 125 edges. On main's sources the wrapper generator from sourcehold#399 makes 24 units
holding 91 source files (93 before), with no internal-name clashes and the same 23 names not on main yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lity-wrappers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kotijekriv and others added 4 commits November 21, 2026 00:44
The entry for UpdateMerchant listed GameStateStructures::activateTraderState. That edge was measured on a 93.8%
draft of UpdateMerchant. The function has since landed on main at 100% (sourcehold#409), and it reaches that under /GL
without the body: activateTraderState.cpp is not on the /GL list. With the body in its unit the caller keeps the
unit slot in esi across the call, where the original reads it again, so the original's caller did not know it.

Measured on main 3458fea with sourcehold#399's wrapper build (-DOPENSHC_FUNCTION_VISIBILITY_UNITS=ON), reccmp, raw:
UpdateMerchant reads 100.0 under /GL, 90.0 with the wrappers before this change and 100.0 after it. It was the only
one of the 150 functions in that build (main's /GL list plus every file the manifest names) whose raw number
differed between /GL and the wrappers, and none differs now.

The manifest now has 85 entries and 124 edges. On main's sources the generator makes 24 units holding 92 source
files (93 before).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…lity-wrappers

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…lity-wrappers

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kotijekriv kotijekriv changed the title cmake: optional wrapper units built from function-visibility.txt (prototype) cmake: optional wrapper units built from function-visibility.txt Nov 21, 2026
@kotijekriv
kotijekriv marked this pull request as ready for review November 21, 2026 00:31
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 21, 2026
UpdateTrebuchet (0x00569410) calls GameSynchronyState::isAIPlayer twice, at 0x0056A05B and 0x0056A124. Behind each
call the original pushes ebx, the unit slot it read from 0xEE0FC8, without reading the slot again (0x0056A068,
0x0056A12D); behind the next call, to 0x00465F20, it reads it again (0x0056A06F, 0x0056A134). So the original's
caller knew that isAIPlayer writes no memory, which it can know only from the body.

Measured with tu_merge.py on main 3458fea (in a clone with two UI commits on top, nothing under Map or Synchrony)
plus worker C's UpdateTrebuchet.cpp (6757ed1, at 100% under /GL), every struct and function resolver on,
quick_diff ratio of the caller:

    caller alone                                          0.5440
    isAIPlayer, hasUnitReachedDestination, deselectUnit   0.9818
    hasUnitReachedDestination, deselectUnit               0.9132
    isAIPlayer alone                                      0.5587

(callees first and caller first read the same in each row). Leaving isAIPlayer out lowers the score, so it is needed
together with the other two. isAIPlayer (0x004010B0) is at a lower address than its caller, and it goes first in the
entry, whose callees are sorted by address.

The manifest now has 85 entries and 125 edges. On main's sources the generator from sourcehold#399 makes the same 24 units
holding 92 source files, file for file, since UpdateTrebuchet is not on main. With that UpdateTrebuchet.cpp in the
build, isAIPlayer.cpp joins its unit (24 units holding 95 source files, 94 without the line); no other file in that
unit calls isAIPlayer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheRedDaemon
TheRedDaemon added this pull request to the merge queue Nov 21, 2026
Merged via the queue into sourcehold:main with commit 02fab42 Nov 21, 2026
8 checks passed
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 21, 2026
The manifest itself is on main since sourcehold#399 (02fab42); this branch keeps
one more edge, UpdateTrebuchet -> GameSynchronyState::isAIPlayer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 24, 2026
…#455)

MenuViews::CrusadeMissionIntro::MenuView_CrusadeMissionIntro_DoEveryFrame, the crusader trail's mission
intro screen: the map preview with the keeps, the lords' gold, the mission's number and name, and the
opponents sorted by team. Written from the original's listing. gynt's best is b4fa695 on his fork's
archive/UI-MenuViews-merged-full (53.54%, "Style pass"; its status line reads 53.5%); his closed sourcehold#253 has
35.8%, and his later 1958ae8 ("Early returns", 42.04%, on reimpl/UI-MenuViews-development, now 45.0% at
466d2ea) has the three returns already. His 08c492b on reimpl/UI-MenuItems-development (38.6%)
writes the id test as `<= 0`, where the original compares with 1 (0x004DE193), so it stays `id < 1` here.

One behaviour fix against b4fa695: when the current trail is complete (extreme 20, war chest 30, first
edition 50 missions) the original draws "Crusader Trail Completed!" and returns (three copies, 0x004DD7F7
to 0x004DD8E2); b4fa695 draws the text and then the whole screen as well. Everything else it does the
way the original does.

What the match needed:

- The title written out per trail and per font: the number, "." and name six times over, as the original
  has them (its six arms end in two shared tails, one per font). With one copy and the font in a local,
  the function came out at 588 instructions to the original's 822.
- The position (y, then x) of the completed-trail message, of the lords' texts and of the mission's name
  in locals right before each call, and the name's index for the width test and for the name itself.
  Written inline, the compiler merges the three message copies the original keeps, hands the scratch
  registers out in another order from the lords' texts on, and the six title arms do not share their
  tails.
- The opponents' list in the original's two paths: the team stored in each arm of the game-suspended
  test, and the no-lord arm counting and continuing on its own. With one count after the test the entries
  are walked through one pointer where the original walks one per field.
- The time tests compare the map time first, as the original does.
- avatarX copied from listX after rowY: copied straight after listX, the border height is read only once
  (the original reads it twice).
- The rows' previous team in its own variable, set with the draw buffer before the two positions: with
  the sort's variable reused, listX, avatarY and the previous team take other stack slots.

Measured in a narrow build on main 3458fea of this file alone (main is now 812d149: sourcehold#399 and sourcehold#398 add
only the optional function-visibility units, off by default), struct resolvers and its resolver on:
100.0% raw and normalised, no diff entry. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
gynt added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 24, 2026
* reimplement: SHC_3BB0A8C1_0x004B5330 100.0% Reimplemented

MinimapViewState::renderMinimapTileStripWithLuminescence, the minimap's second picture: for a strip of
the screen tile rows it takes the colour of what stands on each tile (moat, trees, plain tiles with one
display flag, or the first entry of MiniMapDefinedData's table that shares a bit with the tile's flags),
scales it by the tile's luminescence (RGB555 or RGB565) and writes it into the second minimap buffer, at
full or at half size; a tile without a colour takes the terrain picture's pixel. It is the sibling of
renderMinimapTileColors (0x004B58D0) and is written the same way, from the original's listing.

The function was a 21.4% draft in sourcehold#313 ("Pixel store and registers differ"). sourcehold#313's version started from
gynt's draft as it stood on his fork's reimpl/UI-Core-development, 10280b1 (2026-10-25; status line
"9.3% | Reimplemented, asm differs"), with the body rewritten from the listing. His newest version is
4a1f8dd (2026-11-02, a style pass over the UI folders; "9.0% | Style pass, asm differs") on
reimpl/UI-MenuItems-development, where 1056d3f, 2da2d36 and b60bb72 are earlier steps of it
(9.03%); 24080c9 and 1955a67 (9.0%) are the same draft on reimpl/UI-Core (sourcehold#256). No behaviour change
against sourcehold#313's code (the strip's end is now divided signed, as in the original; the same for every real
argument). What sourcehold#313 changed against every version of the draft stays:

- `mapSize <= 200` where the draft has `< 200` (every 200 by 200 map);
- the return past the strip's end, which the draft lacks (reached by drawMinimap's 2% strips). gynt has it
  himself in 8ca7861 on reimpl/Wilps-2026-10-18;
- the second buffer written through `this`, as in the original, where every version of the draft goes
  through the global instance (the same address while `this` is DAT_MinimapViewState);
- the screen tile table indexed from its base on today's main (not the draft's mistake: its `- 8` was right
  for the header of its day).

What the match needed:

- The pixel stores are handwritten assembly in the original: the same two stores as in
  renderMinimapTileColors, with the address computation of renderMinimapPreview and drawCurrentPixel
  (`add eax, eax` and a `mul edx` by the row pitch, here the constant 400). Two `__asm` blocks, one per
  size. No frame pointer, as in the original.
- The colour is a 16-bit local, and the blue channel and the mix are written out in both colour-mode
  arms. Here the original has one shared end for the two arms: the compiler merges the two identical
  ends itself. Written once behind the arms, the registers of the whole block are different ones.
- The channels initialised blue, green, red; red's and green's masks as statements right after their
  caps, blue's in the mix, as in renderMinimapTileColors.
- The table loop: the entry's bits in a local that is set before the tile's flags are read, the first
  test on that local and the loop's test on the table. The two tests take their operands in the order
  the variables are first set; with the first test on the table and the loop's on the local, both come
  out swapped. A flag tells "no entry" from "found"; the compiler turns it into the two jumps.
  The loop is a guarded do-while: a plain while loop reads 99.7% (one of the two tests swapped, whether
  it tests the local or the table), and a for loop with a break at its end compiles to two instructions
  more.
- The five simple initialisations before the buffer and the strip rows, so that every stack slot is the
  original's; the row base holding the orientation's offset first; the viewport height read from the
  global a second time; the column loop's bound in its test; the tile read before x and y; y inside the
  tile loop and a copy of x at the top of the full-size branch; the row base stepped in the row loop's
  header.

The channel masks have names in a small file-local enum, as in renderMinimapTileColors.

Measured in a narrow build on main 3458fea of this file alone (main is now 812d149: sourcehold#399 and sourcehold#398 add
only the optional function-visibility units, off by default), struct resolvers and its resolver on:
100.0% raw and normalised, no diff entry. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* headers: ColorChannelMask, the channel bits of an RGB555 and an RGB565 colour

A new enum in OpenSHC::Rendering beside ColorMode: the masks of the red, green and blue channels in the two
colour modes. renderMinimapTileStripWithLuminescence (and renderMinimapTileColors) scale each channel of a
minimap colour by the tile's light and cap it at these masks; they had them as a file-local enum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* renderMinimapTileStripWithLuminescence: name the numbers by main's constants

Review: the channel masks come from the new ColorChannelMask header enum; the map's 400 rows and its 200-tile
size from Map::MapConstants (MC_MAP_XY_LIMIT, MC_MAP_SIZE_200); the screen tile table's start, its two row
lengths (200 and 201) and its 80400 entries per orientation from Rendering::Enums::ViewportConstants. The
orientation offsets are VC_SCREEN_POINT_TABLE_START + k * VC_SCREEN_POINT_TABLE_SIZE, and 401 is a short and a
long row. The two inline assembly blocks take the buffer's row length from a file-local enum, since inline
assembly cannot name a qualified constant.

The same code: the quick compile's listing is unchanged, and a narrow build of this file reads 100.0 raw and
normalised with no diff entry.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* renderMinimapTileStripWithLuminescence: the small map's margin and the buffer's row from map constants

Review: the first row for a map of up to 200 tiles is the margin around it, (MC_MAP_XY_LIMIT -
MC_MAP_SIZE_200) / 2, and the assembly's row length is a buffer row of MC_MAP_XY_LIMIT / 2 pixels in bytes
(both minimap buffers are 400 rows of 200 ushorts).

The same code: the quick compile's listing is unchanged, and a narrow build of this file reads 100.0 raw and
normalised with no diff entry.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 24, 2026
* reimplement: SHC_3BB0A8C1_0x004B58D0 100.0% Reimplemented

MinimapViewState::renderMinimapTileColors, the minimap's terrain picture: for a strip of the screen tile
rows it takes each tile's colour from its terrain flags and MiniMapDefinedData's tables, scales it by the
tile's luminescence (RGB555 or RGB565) and writes it into the minimap pixel buffer, at full or at half
size. Written from the original's listing. gynt's draft is 4a1f8dd (2026-11-02, a style pass over the
UI folders; status line "20.4% | Style pass, asm differs") on his fork's reimpl/UI-MenuItems-development;
his 0b3142f, 1de07cb and 0d4e470 are earlier steps of it at 20.36%. 10280b1 on
reimpl/UI-Core-development has 20.2%, and a0e86e1 (20.4%) and ab8f1e3 (20.2%) are the same draft on
his reimpl/UI-Core (sourcehold#256). The draft has the tables' names and the whole colour chain as the original has
it.

Behaviour fixes against 4a1f8dd:

- the map-size test is `mapSize < 200` where the original has `<= 200` (0x004B597E: `cmp ..., 200; jg`),
  so a 200-tile map took the larger maps' path, where the column count is halved: rows 0 to 399 and
  columns 0 to 99 of the screen tile table, where the original draws the table's centre, rows 100 to 299
  and columns 50 to 149 (every 200 by 200 map);
- past the strip's end the original returns (0x004B5A6B); the draft has an empty `if` there and draws on
  (no caller passes a partial strip today). gynt has that return himself in 8ca7861 on
  reimpl/Wilps-2026-10-18.

Two places where the code differs from the draft without a change on today's callers:

- the pixels are written through `this->minimapPixelBuffer`, as in the original, not through the fixed
  address 0x01A58754, the same address while `this` is DAT_MinimapViewState;
- the screen tile table is indexed from its base on today's main (the draft's `- 8` was right for the
  header of its day and reads 8 entries too low now).

What the match needed:

- The pixel stores are handwritten assembly in the original: `add eax, eax` and a `mul edx` by the row
  pitch, the same address computation as in renderMinimapPreview and drawCurrentPixel (whose pitch is
  `horizontalByteSize`, not the constant 400). Here they are two `__asm` blocks, one per size. The
  function keeps no frame pointer with them, as in the original.
- The colour is a 16-bit local. As a uint, the two colour modes' ends are merged into one block and the
  (ushort) of the mix is split over its operands; the original has both ends written out.
- The channels: each mask as a statement right after its cap, and green set before red (the mix takes
  the two in the order they are first set, so the three are initialised blue, green, red).
- The five simple initialisations before the two strip-row divisions: a local set while a parameter is
  still to be read cannot take its stack slot. With them first, every stack slot is the original's (the
  row counter, the colour and x's stepping value sit in the dead parameters' slots).
- The viewport height read from the global a second time for the other parity; y declared inside the
  tile loop and a copy of x at the top of the full-size branch, so that the stores of the assembly's
  operands stand where the original has them; the tile read before x; rowBase stepped in the row loop's
  header; the column loop started at `firstRow / 2` (started at firstColumn, the compiler folds x's first
  value, which the original computes as `(firstColumn - firstColumn) * widthFactor + rowX`); rowBase
  holding the orientation's offset first, with north writing the 8 again.

The channel masks have names in a small file-local enum (the style lint asks for names or decimals);
the listing is the same with the hex numbers.

Measured in a narrow build on main 3458fea of this file alone (main is now 812d149: sourcehold#399 and sourcehold#398 add
only the optional function-visibility units, off by default), struct resolvers and its resolver on:
100.0% raw and normalised, no diff entry. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* style: renderMinimapTileColors names its map, buffer and screen tile table sizes

The three 400s in the C++ (the strip's first and end rows, and the number of columns) are the map's 400 screen tile
rows and columns, Map::MC_MAP_XY_LIMIT. The assembly's two `mov edx, 400` are a row of the minimap buffer in bytes
(200 pixels of 2 bytes): a file-local BUFFER_ROW_BYTES, defined as MC_MAP_XY_LIMIT / 2 * sizeof(ushort), as
renderMinimapTileStripWithLuminescence in sourcehold#465 does, because inline assembly cannot name a qualified constant.

The screen tile table's numbers use main's ViewportConstants, as sourcehold#465 writes them: the table's start (8) is
VC_SCREEN_POINT_TABLE_START, the other orientations' tables (80408, 160808, 241208) are VC_SCREEN_POINT_TABLE_START
plus one, two and three VC_SCREEN_POINT_TABLE_SIZE, a short and a long row together (401) are
VC_SCREEN_POINT_SHORT_ROW + VC_SCREEN_POINT_LONG_ROW, and the row lengths (200, 201) are those two. The 200 of the
row count and of the map size test is Map::MC_MAP_SIZE_200, and such a map's first row (100) is (MC_MAP_XY_LIMIT -
MC_MAP_SIZE_200) / 2. The comment above the orientation test names the table's start instead of the 8. The file
includes MapConstants.hpp and ViewportConstants.hpp. No other change.

The colour masks stay a file-local enum for now: Rendering::ColorChannelMask, which names them, comes with sourcehold#465,
and this file switches to it once sourcehold#465 is on main.

Measured: with the precompiled header, quick_diff gives the same listing for the pushed head's text and this one
after MSVC's `$T` temp labels are normalised. A narrow real build (main's /GL list and this round's files of two of
our PRs, with the struct resolvers and those files' function resolvers on, sourcehold's reccmp with
--resolve-wrapped-calls --ignore-security-cookie) reads 100.0% raw and normalised with no differing line for each
function this commit touches, and every other function reads as in the same build without them.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* headers: MinimapConstants, with a row of the minimap buffer in bytes

A new generated-form enum, UI::Enums::MinimapConstants, with one value: MMC_BUFFER_ROW_BYTES = 400, a row of
MinimapViewState's pixel buffer (200 pixels of 2 bytes; the buffer is ushort[80000], 400 such rows).
renderMinimapTileColors' handwritten pixel stores multiply the row by it (`mov edx, 400; mul edx`, 0x004B5E68 and
0x004B5F01). Asked for on the review, in place of the file-local constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* renderMinimapTileColors uses ColorChannelMask's and MinimapConstants' names

The file-local colour mask enum goes: main has Rendering::ColorChannelMask now, and the five masks are its
CCM_RGB555_RED, CCM_RGB555_GREEN, CCM_RGB565_RED, CCM_RGB565_GREEN and CCM_RGB_BLUE, named with using-declarations as
renderMinimapTileStripWithLuminescence does. The file-local BUFFER_ROW_BYTES goes too: the assembly's two row
multiplies use MinimapConstants' MMC_BUFFER_ROW_BYTES, through a using-declaration, since inline assembly cannot
name a qualified constant but does take the unqualified name.

The function compiles to the same code: its listing is the same instruction for instruction once MSVC's numbering
of locals and temporaries is normalised, and a narrow real build reads 100.0% raw and normalised.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
6and8 pushed a commit to 6and8/OpenSHC that referenced this pull request Nov 24, 2026
… bodies (sourcehold#398)

* cmake: function-visibility.txt, which callers must see which callees' bodies

One entry per caller, with the callees whose bodies it must see when it is
compiled, fully qualified as the definitions are written:

    OpenSHC::funcA
        OpenSHC::funcB

87 entries, 127 edges, sorted by the caller's address. Every edge is a
tu_merge.py measurement: pasting the callee into the caller's unit raises the
caller's quick_diff ratio, and for callers with several callees a leave-one-out
over the full set decided which are needed together. In all 127 the callee is
at a lower address than its caller.

Twelve measured gains are left out because they cannot be same-file edges:
in seven the callee is at a higher address than its caller, and the other
five would put a callee into a unit with callers that measure worse when they
see it.

23 names are not on main yet. 22 are Map::Units Update callers: they were
measured on our local branches reimpl/units-B-development and
reimpl/units-A-development (UpdateCamel is also on the local
reimpl/units-B-round2-100), and gynt's reimpl/map-units-updatefunctions has
drafts of all 22. The 23rd is UnitsState::changeDestinationByAmount from sourcehold#378.
A generator skips names it cannot find. The measurements are in the PR
description.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* cmake: Gibbet and StretchingRack no longer need updateBuildingSignpostCounter's body

sourcehold#290 rewrote UpdateBadBuildingGibbet and UpdateBadBuildingStretchingRack so that they reach 100% without /GL. On
main (ea63a49), tu_merge.py gives both 1.0000 in all three layouts: the caller alone, the callee's body pasted
before it, and pasted after it (45 of 45 and 41 of 41 instructions). Seeing updateBuildingSignpostCounter no longer
helps either of them, so their two entries go. The callee keeps its other two callers, UpdateBadBuildingGallows and
UpdateBadBuildingDungeon.

The manifest now has 85 entries and 125 edges. On main's sources the wrapper generator from sourcehold#399 makes 24 units
holding 91 source files (93 before), with no internal-name clashes and the same 23 names not on main yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* cmake: UpdateMerchant must not see activateTraderState's body

The entry for UpdateMerchant listed GameStateStructures::activateTraderState. That edge was measured on a 93.8%
draft of UpdateMerchant. The function has since landed on main at 100% (sourcehold#409), and it reaches that under /GL
without the body: activateTraderState.cpp is not on the /GL list. With the body in its unit the caller keeps the
unit slot in esi across the call, where the original reads it again, so the original's caller did not know it.

Measured on main 3458fea with sourcehold#399's wrapper build (-DOPENSHC_FUNCTION_VISIBILITY_UNITS=ON), reccmp, raw:
UpdateMerchant reads 100.0 under /GL, 90.0 with the wrappers before this change and 100.0 after it. It was the only
one of the 150 functions in that build (main's /GL list plus every file the manifest names) whose raw number
differed between /GL and the wrappers, and none differs now.

The manifest now has 85 entries and 124 edges. On main's sources the generator makes 24 units holding 92 source
files (93 before).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* cmake: UpdateTrebuchet must see isAIPlayer's body

UpdateTrebuchet (0x00569410) calls GameSynchronyState::isAIPlayer twice, at 0x0056A05B and 0x0056A124. Behind each
call the original pushes ebx, the unit slot it read from 0xEE0FC8, without reading the slot again (0x0056A068,
0x0056A12D); behind the next call, to 0x00465F20, it reads it again (0x0056A06F, 0x0056A134). So the original's
caller knew that isAIPlayer writes no memory, which it can know only from the body.

Measured with tu_merge.py on main 3458fea (in a clone with two UI commits on top, nothing under Map or Synchrony)
plus worker C's UpdateTrebuchet.cpp (6757ed1, at 100% under /GL), every struct and function resolver on,
quick_diff ratio of the caller:

    caller alone                                          0.5440
    isAIPlayer, hasUnitReachedDestination, deselectUnit   0.9818
    hasUnitReachedDestination, deselectUnit               0.9132
    isAIPlayer alone                                      0.5587

(callees first and caller first read the same in each row). Leaving isAIPlayer out lowers the score, so it is needed
together with the other two. isAIPlayer (0x004010B0) is at a lower address than its caller, and it goes first in the
entry, whose callees are sorted by address.

The manifest now has 85 entries and 125 edges. On main's sources the generator from sourcehold#399 makes the same 24 units
holding 92 source files, file for file, since UpdateTrebuchet is not on main. With that UpdateTrebuchet.cpp in the
build, isAIPlayer.cpp joins its unit (24 units holding 95 source files, 94 without the line); no other file in that
unit calls isAIPlayer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
6and8 pushed a commit to 6and8/OpenSHC that referenced this pull request Nov 24, 2026
* reimplement: SHC_3BB0A8C1_0x0042C620 100.0% Reimplemented

Actions::LaunchSinglePlayerGameUnk starts a single-player game on a map: it loads the map picked in the
map choice (or, called with 1, the map whose name standaloneFilename already holds), sets what can be made
and recruited, applies the mission type's rules and opens the first menu view. For a siege that is the
map's siege engines as an attack wave scaled by the difficulty, the attacker's troops (the ones picked in
the map choice in the advanced mode, otherwise 70% of the map's for a player in slot 2) and no peasants;
for an invasion on difficulty 1 to 3 (from the map choice always 1: its start button sets it), on a map
for which mapHasCertainEvent says no, it takes every
fourth, third or second of the player's own units away until a quarter, a third or half of their troop
value is gone. 663 instructions, written from the original's listing.

The fields it uses come from gynt's draft, under main's names today. The draft goes back to ce7ebb0
(2026-10-02, "reimplement: SHC_3BB0A8C1_0x0042C620 25.26% Style pass, asm differs"). Its best score, 28.35%,
is ca39d7b (2026-10-03, the same subject), where he merged the draft's split name buffer (`char
local_7e0[4]` and a `local_7dc[1004]` that nothing used) into one of 1008 bytes; his 409e2a3 ("Split
buffer merged", on sourcehold's own reimpl/UI-Core) makes the same change and says why. The newest version
is 15e5712 (2026-11-02, "style: rule 12, import 76 string literals across UI/Rendering and Helpers") on
his fork's reimpl/UI-MenuItems-development, whose status line reads "28.4% | Style pass, asm differs".
reimpl/UI-Core-development has it at 27.1% (10280b1) and reimpl/UI-Core (sourcehold#256) at 26.3% (2f1c52c);
both still have the 4-byte name buffer.
That draft does not compile on today's main: DAT_SiegeInformationArray is `int[7][3]` now, and two
SiegeUnitCounts fields, Building's tower engine field and PlayerData's keep entry have other names.

No behaviour change against the draft: read block by block, both do the same. One thing a literal port
would get wrong: the draft reads the picked map from DAT_ArrayOfMapIndices[scroll offset + selection - 1],
which was right for the header of its day (the array started one entry later); on main the array starts
where the original reads it, so the index has no `- 1`. gynt's own newer export (8ca7861 on
reimpl/Wilps-2026-10-18) has it without the `- 1` too.

What the match needed, each measured against its alternative:

- The two name buffers are char[1005] and char[1001] (the frame's size, as gynt found in 409e2a3).
- The first six "can be made" stores in the order crossbow, pike, sword, bow, spear, mace; in ascending
  order two of the stores move.
- The picked map's index as selection + scroll offset; the other way round swaps the two loads.
- The nine recruitable flags cleared in front of the loop that copies the fourteen recruitable values.
  Behind it the loop takes the register that holds the function's zero, and the zero is no longer in a
  register from the first test of the argument on.
- The siege numbers are zeroed by their own `if (type != siege)` in front of the siege's peasant loop. As
  the else arm of that loop's test, the three blocks before it stay in the written order; the original
  has them behind the keep's part of the function.
- The twenty siege troop counts are walked as an array in all three places (copy, 70%, zero). The header
  has twenty named fields; twenty written zero stores take the function's zero register where the
  original makes a new one.
- In the invasion block the unit counter is declared in front of the value total; the other way round
  the two are set to zero in the other order.
- The last menu call written in each arm; one call with a `? :` argument computes the number without a
  jump.
- The argument is compared with 1 at the end, as in the original; a truth test compares with zero.

The mission types (0 just build, 1 economic, 2 siege, 3 invasion) are plain numbers with a comment, as in
main's other files: DAT_MapMissionType has no enum, and a file-local one changes two registers of the
second map name call (declared alone, without a single use).

Measured in a narrow build on main 3458fea of this file alone (main is now 812d149: sourcehold#399 and sourcehold#398 add
only the optional function-visibility units, off by default), struct resolvers and its resolver on:
100.0% raw and normalised, no diff entry. Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* LaunchSinglePlayerGameUnk: TRUE and FALSE for the flags it sets

Review: the six "can be made" settings (MapPropertiesState's SEC_*Producible_save shorts and GameCore's
*Producible_logic ints) and the nine euroRecruitableCopy_* flags are only ever 0 or 1 in the original: every
store is a 1 or a 0, a toggle with `xor 1` (the map editor) or a copy between the two sets of fields, and every
read tests against 0. They now take TRUE and FALSE. Main has no 16-bit BOOLEnum, so the short fields keep their
type; no header changes. One comment says what the nine flags are.

The same code: the quick compile's listing is unchanged, and a narrow build of this file reads 100.0 raw and
normalised with no diff entry.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* LaunchSinglePlayerGameUnk: index euroRecruitable by unit type

Review: euroRecruitable is indexed by the unit type counted from UT_E_ARCHER (main's IsEuroUnitRecruitableUnk
reads it as euroRecruitable[unitType - UT_E_ARCHER]), so the seven tests behind the copy loop are written
euroRecruitable[UT_E_XBOW - UT_E_ARCHER] and so on.

The same code: the quick compile's listing is unchanged, and a narrow build of this file reads 100.0 raw and
normalised with no diff entry.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants